package org.zh.sh.util.create;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import static org.zh.sh.config.GlobalStatic.SQlite_URL;
import static org.zh.sh.config.GlobalStatic.mysql_URL;

public interface TablesInfo {
    String split = "_";
    String mysqlPri = "PRI";
    String mysql_auto = "AUTO_INCREMENT";


   String mysqlType = "mysql";
   String SQLiteType = "SQLite";

    List<String> showTables(Connection conn) throws SQLException;

    List<TableInfo> allColumnsInfo(Collection<String> tableNames, Connection conn) throws SQLException;

    TableInfo columnsInfo(String tableName, Connection conn) throws SQLException;

    List<String> tablePri(String tableName,Connection conn) throws SQLException;

    default String clazzAlias(String column) {
        String[] words = column.split(split);
        StringBuilder clazzAlias = new StringBuilder();
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            if (i == 0) {
                clazzAlias.append(word);
            } else {
                clazzAlias.append(Character.toUpperCase(word.charAt(0)));
                if (word.length() > 1) {
                    clazzAlias.append(word.substring(1));
                }
            }
        }
        return clazzAlias.toString();
    }

    default String clazzName(String column) {
        String[] words = column.split(split);
        StringBuilder clazzName = new StringBuilder();
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            if (i == 0) {
                clazzName.append(Character.toUpperCase(word.charAt(0))).append(word.substring(1));
            } else {
                clazzName.append(Character.toUpperCase(word.charAt(0)));
                if (word.length() > 1) {
                    clazzName.append(word.substring(1));
                }
            }
        }
        return clazzName.toString();
    }

    static Connection getConn(String dbType) {
        try {
            switch (dbType) {
                case SQLiteType -> {
                    return DriverManager.getConnection(SQlite_URL);
                }
                case mysqlType -> {
                    return DriverManager.getConnection(mysql_URL);
                }
                default -> throw new IllegalStateException("Unexpected value: " + dbType);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
